﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace EnvironSystem
{
    public class EnvironmentManager:IEnvironmentManager
    {
        internal Dictionary<int, List<IEnvironmentElement>> id2Elements = new Dictionary<int, List<IEnvironmentElement>>();

        Dictionary<AreaType, AreaComputerBase> areaComputers;

        Dictionary<int, List<ISpawn>> id2Spawns = new Dictionary<int, List<ISpawn>>();


        public void Init()
        {
            areaComputers = new Dictionary<AreaType, AreaComputerBase>() { { AreaType.Circle, new CircleAreaComputer(this) } };
        }

        public IEnvironmentElement[] GetElementsInArea(AreaType type,IAreaArgs args)
        {
            if (areaComputers.ContainsKey(type))
            {
                return areaComputers[type].GetElementsInArea(args);
            }
            Debug.LogError("未配置  "+ type + "  相应的区域计算器");
            return null;
        }

        public void RegisterElement(int id, IEnvironmentElement element )
        {
            if (!id2Elements.ContainsKey(id))
            {
                id2Elements.Add(id,new List<IEnvironmentElement>() { element });
            }
            else{
                id2Elements[id].Add(element);
            }
        }

        public void RemoveElement(int id, IEnvironmentElement element)
        {
            if (id2Elements.ContainsKey(id))
            {
                id2Elements[id].Remove(element);
            }
            else
            {
                Debug.LogError("未注册");
            }
        }

        public void RegisterSpawn(int id, ISpawn spawn)
        {
            if (!id2Spawns.ContainsKey(id))
            {
                id2Spawns.Add(id, new List<ISpawn>() { spawn });
            }
            else
            {
                id2Spawns[id].Add(spawn);
            }
        }

        public object Create(int id,string tag)
        {
            if (id2Spawns.ContainsKey(id))
            {
                for (int i = 0; i < id2Spawns[id].Count; i++)
                {
                    if (tag== id2Spawns[id][i].SpawnTag)
                    {
                        return id2Spawns[id][i].Create();
                    }
                }
            }
            else
            {
                Debug.LogError("未注册");
            }
            return null;
        }

        public void RemoveSpawn(int id, ISpawn spawn)
        {
            if (id2Spawns.ContainsKey(id))
            {
                id2Spawns[id].Remove(spawn);
            }
            else
            {
                Debug.LogError("未注册");
            }
        }
    }
}
